<template>
	<view class="app-case">
		<view class="app-item" v-for="(item, index) in packageList" :key="index">
			<view class="item-header">
				<text class="item-title">{{ item.title }}</text>
				<text class="item-tip" @click="viewDetails(item.packageid)">查看详情</text>
			</view>
			<view class="item-content">
				<text class="item-detail">{{ item.shorttitle }}</text>
				<text class="item-desc">{{ item.description }}</text>
			</view>
			<view class="item-row">
				<image :src="imgUrl(item.image)" mode="aspectFill" />
				<view class="item-btngroup">
					<!-- <view class="item-button upgrade-btn">升级</view> -->
					<view class="item-button select-btn" :class="item.status?'select-btn-disabled':''"
						@click="selectPackage(item)">选择</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script setup>
	import {
		ref,
		computed
	} from 'vue'
	import {
		imgUrl
	} from "../../../utils/global";
	import {
		useOrderStore
	} from '@/store/order';
import {
	useUserStore
} from '@/store/user';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		nextTick
	} from 'vue';
	import {
		getCaseListApi
	} from '@/api/case';
	import {
		updataCheckUp
	} from '@/api/order';
	const userStore=useUserStore()
	const orderStore = useOrderStore();
	onLoad(async () => {
		await nextTick();
		try {
			const results = await getCaseListApi({
				id: orderStore.customerInfo.id
			});
			packageList.value=[]
			results.forEach(item => {
				item['image'] = [0, 1].includes(Number(item.sex)) ? '/static/img/icons/male.png' :
					'/static/img/icons/female.png';
				item['description'] = '';//阿斯加德卢卡斯进度款拉手机考拉金石可镂大理石肯德基立卡手机到啦
				item['status'] = false
				if (item.sex == 0) {
					if (orderStore.customerInfo.gender != 0) {
						item['status'] = true
					}
				} else {
					if (orderStore.customerInfo.gender != item.sex) {
						item['status'] = true
					}
					if (orderStore.customerInfo.ismarried != item.ismarried) {
						item['status'] = true
					}
				}
				if(!item['status']){
					packageList.value.push(item)
				}
			});
		} catch (error) {
			console.error('getCaseList error', error);
		}
	});

	const packageList = ref([]);

	// 选择套餐
	const selectPackage = (packageInfo) => {
		if (orderStore.customerInfo.gender != packageInfo.sex) {
			uni.showToast({
				icon: "none",
				title: "性别与所选套餐不符"
			})
			return
		}
		if (orderStore.customerInfo.gender != 0 && packageInfo.ismarried != orderStore.customerInfo.ismarried) {
			uni.showToast({
				icon: "none",
				title: "婚姻状态与所选套餐不符"
			})
			return
		}
		uni.showToast({
			title: '选择套餐中...',
			icon: 'loading'
		});
		try {
			orderStore.setPackage(packageInfo);
					uni.hideLoading();
					// 跳转到详情页面
					uni.redirectTo({
						url: '/pages/case/overview/index'
					});
				
			// updataCheckUp({
			// 	id: orderStore.customerInfo.id,
			// 	packageid:orderStore.selectedPackage.packageid
			// }).then(res => {
			// 	orderStore.setPackage(packageInfo);
			// 	uni.hideLoading();
			// 	// 跳转到详情页面
			// 	uni.redirectTo({
			// 		url: '/pages/case/overview/index'
			// 	});
			// })
		} catch (error) {
			uni.hideToast();
			uni.showToast({
				title: '选择失败',
				icon: 'error'
			});
			console.error('选择套餐失败:', error);
		}
	};

	// 查看详情
	const viewDetails = (id) => {
		uni.navigateTo({
			url: `/pages/case/detail/index?id=${id}`
		})
	};
</script>
<style src="./style.scss" scoped />